package com.etone.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.etone.common.core.domain.AjaxResult;
import com.etone.common.utils.DateUtils;
import com.etone.common.utils.http.HttpUtils;
import com.etone.system.domain.*;
import com.etone.system.mapper.*;
import com.etone.system.service.AgvService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AgvServiceImpl implements AgvService {

    @Autowired
    private FiveTaskinfoMapper fiveTaskinfoMapper;

    @Autowired
    private SysDibiaoMapper dibiaoMapper;

    @Autowired
    private AgvMapper agvMapper;

    @Autowired
    private SysStorageMapper sysStorageMapper;

    @Autowired
    private FiveCtuRecordMapper fiveCtuRecordMapper;

    @Autowired
    private SixFreetaskMapper sixFreetaskMapper;

    private static String rcsUrl = "http://172.16.0.200:8182/rcms/services/rest/hikRpcService/";

    public static AjaxResult callAgv(List<TurnoverInventory> list,String type,FiveTaskinfoMapper fiveTaskinfoMapper,
                                     SysDibiaoMapper dibiaoMapper,AgvMapper agvMapper,SysStorageMapper sysStorageMapper,
                                     FiveCtuRecordMapper fiveCtuRecordMapper,SixWuliaoinfoMapper sixWuliaoinfoMapper,
                                     ITurnoverInventoryMapper iTurnoverInventoryMapper,SixFreetaskMapper sixFreetaskMapper) {
        try{
            TurnoverInventory turnoverInventory = list.get(0);
            String huojiano = turnoverInventory.getCangweiNo().substring(0,6);

//            SysStorage sysStorage = new SysStorage();
//            sysStorage.setLieType(huojiano);
//            List<Map<String,Object>> lists = sysStorageMapper.selectSysStorageList(sysStorage);
//            if(lists.size()!=0){
//                for(Map<String,Object> map1 : lists){
//                    String aa = gainJieStr(map1.get("cangweiNo").toString());
//                    System.out.println("仓位解绑接口返回值======="+aa);
//                }
//            }

            //获取入库周转区的有空闲位置的地码
            String startPosition = "";
            String endPosition = "";
            String taskModelType = "";
            String taskType = "";
            String a = "";
            if(type.equals("0")){//入库
                //判断货架号来源于入库位还是出库位
                a = gainRorCweizhiInfo(huojiano,dibiaoMapper);
                Map<String,Object> map1 = null;
                taskModelType = "Q1";
                if(a.equals("1")){
                    taskType = "入库";
                    map1 = gainweizhiInfo(huojiano,1l,2l,dibiaoMapper);
                }else if(a.equals("4")){
                    badwuliaobusiness(sixWuliaoinfoMapper,sysStorageMapper,iTurnoverInventoryMapper,list,huojiano);
                    taskType = "出库位到入库周转区";
                    map1 = gainweizhiInfo(huojiano,4l,2l,dibiaoMapper);
                }else{
                    return AjaxResult.error("货架来源路径不符！");
                }
                startPosition = map1.get("startPosition").toString();
                endPosition = map1.get("endPosition").toString();
            }
//            else if(type.equals("1")){//出库
//                taskType = "出库";
//                //对pda扫出的物料进行遍历，获取物料存储在ctu的原始位置
//                callCCTUAgv(list,fiveCtuRecordMapper,sysStorageMapper,agvMapper,fiveTaskinfoMapper,dibiaoMapper);
//            }
            else if(type.equals("2")){//入库位直接到出库位
                taskModelType = "Q3";
                taskType = "加急潜伏---入库位直接到出库位";
                Map<String,Object> map1 = gainweizhiInfo(huojiano,1l,4l,dibiaoMapper);
                startPosition = map1.get("startPosition").toString();
                endPosition = map1.get("endPosition").toString();
            }
            Gson gson = new Gson();
            String json1 = gson.toJson(list);
            if(endPosition.equals("")){
                //判断物料号是否已经在物料表里面了，如果在则生成空闲任务
                TurnoverInventory turnoverInventory1 = list.get(0);
                String wuliaoNo = turnoverInventory1.getWuliaoNo();
                SixWuliaoinfo sixWuliaoinfo = new SixWuliaoinfo();
                sixWuliaoinfo.setWuliaoCode(wuliaoNo);
                List<SixWuliaoinfo>  wuliaolist = sixWuliaoinfoMapper.selectSixWuliaoinfoList(sixWuliaoinfo);
                if(wuliaolist.size() == 0){
                    SixFreetask sixFreetask = new SixFreetask();
                    sixFreetask.setType(type);
                    sixFreetask.setTaskModeltype(taskModelType);
                    sixFreetask.setWuliaoInfo(json1.toString());
//                sixFreetask.setStartp();
//                sixFreetask.setEndp();
//                sixFreetask.setHuojia();
//                sixFreetask.setStartText();
//                sixFreetask.setEndText();
                    sixFreetaskMapper.insertSixFreetask(sixFreetask);
                }
                return AjaxResult.success("调用成功！");
            }
            FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
            fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
            fiveTaskinfo.setTaskStartPosition(startPosition);
            fiveTaskinfo.setTaskEndPosition(endPosition);
            fiveTaskinfo.setTaskType(taskType);

            //现将绑定仓位接口写在这里
            for(TurnoverInventory turnoverInventory1 : list){
                String aa = gainStr(turnoverInventory1.getCangweiNo());
                System.out.println("仓位绑定接口返回值======="+aa);
            }

            String response = "";
            String param = createParamInfo(taskModelType,startPosition,endPosition);
            JSONObject json = JSONObject.parseObject(param);
            String reqCode = UUID.randomUUID().toString().substring(0, 20);
            json.fluentPut("reqCode", reqCode);
            json.fluentPut("reqTime", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
            json.fluentPut("taskCode", reqCode);

            fiveTaskinfo.setTaskCode(reqCode);
            fiveTaskinfo.setTaskStartInfo(json.toString());
            response = HttpUtils.sendRestPost(rcsUrl+"genAgvSchedulingTask",json);
            fiveTaskinfo.setTaskEndInfo("普通背货架agv执行---"+response);
            fiveTaskinfo.setStatus("待完成");
            fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
            fiveTaskinfo.setRemark(json1.toString()+"---"+a);
            fiveTaskinfoMapper.insertFiveTaskinfo(fiveTaskinfo);
            return AjaxResult.success("调用成功！");
        }catch (Exception e){
            return AjaxResult.error("调用失败！");
        }
    }
    /**
     * 存在bad物料的处理业务
     * */
    public static void badwuliaobusiness(SixWuliaoinfoMapper sixWuliaoinfoMapper,SysStorageMapper sysStorageMapper,ITurnoverInventoryMapper iTurnoverInventoryMapper,
                                         List<TurnoverInventory> list,String huojiahao){
        //发送好物料解绑任务
//        SixWuliaoinfo sixWuliaoinfo = new SixWuliaoinfo();
//        sixWuliaoinfo.setHuojiaCode(huojiahao);
//        List<SixWuliaoinfo> selectSixWuliaoinfoList =
//                sixWuliaoinfoMapper.selectSixWuliaoinfoList(sixWuliaoinfo);
//        for(SixWuliaoinfo sixWuliaoinfo1 : selectSixWuliaoinfoList){
//            String aa = gainJieStr(sixWuliaoinfo1.getStorageCode());
//            System.out.println("仓位解绑接口返回值======="+aa);
//        }

        //将货架关联的好物料信息清空，物料溯源结束
        sixWuliaoinfoMapper.deleteSixWuliaoinfoByhuojiacode(huojiahao);
        for(TurnoverInventory turnoverInventory : list){
            sixWuliaoinfoMapper.deleteSixWuliaoinfoBywuliaocode(turnoverInventory.getWuliaoNo());
        }
        sixWuliaoinfoMapper.insertSixWuliaoinfoList(list);
        //将出库货架所有关联好物料货位状态为free
        bindcangweiornobindByhuojia(sysStorageMapper,huojiahao, 0, 0);
        iTurnoverInventoryMapper.addTurnoverInventory(list);
        //  根据仓位更改存储状态
        iTurnoverInventoryMapper.updateSysStorage(list);
    }

    public static String gainJieStr(String cangweino){
        String response = "";
        String param = "{\n" +
                "    \"reqCode\": \"\",\n" +
                "    \"reqTime\": \"\",\n" +
                "    \"clientCode\": \"\",\n" +
                "    \"tokenCode\": \"\",\n" +
                "    \"ctnrTyp\": \"1\",\n" +
                "    \"ctnrCode\": \"\",\n" +
                "    \"wbCode\": \"\",\n" +
                "    \"podCode\": \"\",\n" +
                "    \"podDir\": \"\",\n" +
                "    \"podTyp\": \"\",\n" +
                "    \"materialLot\": \"\",\n" +
                "    \"agvCode\": \"\",\n" +
                "    \"taskCode\": \"\",\n" +
                "    \"stgBinCode\": \""+cangweino+"\",\n" +
                "    \"indBind\": \"0\",\n" +
                "    \"data\": \"\"\n" +
                "}";
        JSONObject json = JSONObject.parseObject(param);
        String reqCode = UUID.randomUUID().toString().substring(0, 20);
        json.fluentPut("reqCode", reqCode);
        json.fluentPut("reqTime", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
        json.fluentPut("taskCode", reqCode);
        response = HttpUtils.sendRestPost(rcsUrl+"bindCtnrAndBin",json);
        return response;
    }

    public static void bindcangweiornobindByhuojia(SysStorageMapper sysStorageMapper,String position, long isrequest, long isfree) {
        SysStorage sysStorage = new SysStorage();
        sysStorage.setLieType(position);
        List<Map<String, Object>> list = sysStorageMapper.selectSysStorageList(sysStorage);
        for (Map<String, Object> map : list) {
            sysStorage.setCangweiNo(null);
            sysStorage.setId(Long.parseLong(map.get("id").toString()));
            sysStorage.setIsRequest(isrequest);
            sysStorage.setIsFree(isfree);
            sysStorageMapper.updateSysStorage(sysStorage);
        }
    }

    public static String gainStr(String cangweino){
        String response = "";
        String param = "{\n" +
                "    \"reqCode\": \"\",\n" +
                "    \"reqTime\": \"\",\n" +
                "    \"clientCode\": \"\",\n" +
                "    \"tokenCode\": \"\",\n" +
                "    \"ctnrTyp\": \"1\",\n" +
                "    \"ctnrCode\": \"\",\n" +
                "    \"wbCode\": \"\",\n" +
                "    \"podCode\": \"\",\n" +
                "    \"podDir\": \"\",\n" +
                "    \"podTyp\": \"\",\n" +
                "    \"materialLot\": \"\",\n" +
                "    \"agvCode\": \"\",\n" +
                "    \"taskCode\": \"\",\n" +
                "    \"stgBinCode\": \""+cangweino+"\",\n" +
                "    \"indBind\": \"1\",\n" +
                "    \"data\": \"\"\n" +
                "}";
        JSONObject json = JSONObject.parseObject(param);
        String reqCode = UUID.randomUUID().toString().substring(0, 20);
        json.fluentPut("reqCode", reqCode);
        json.fluentPut("reqTime", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
        json.fluentPut("taskCode", reqCode);
        response = HttpUtils.sendRestPost(rcsUrl+"bindCtnrAndBin",json);
        return response;
    }

    public static String createParamInfo(String taskModelType,String startPosition,String endPosition){
        String param = "{\n" +
                "    \"reqCode\": \"\",\n" +
                "    \"reqTime\": \"\",\n" +
                "    \"clientCode\": \"\",\n" +
                "    \"tokenCode\": \"\",\n" +
                "    \"taskTyp\": \""+taskModelType+"\",\n" +
                "    \"ctnrTyp\": \"\",\n" +
                "    \"ctnrCode\": \"\",\n" +
                "    \"wbCode\": \"\",\n" +
                "    \"positionCodePath\": [\n" +
                "        {\n" +
                "            \"positionCode\": \""+startPosition+"\",\n" +
                "            \"type\": \"00\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"positionCode\": \""+endPosition+"\",\n" +
                "            \"type\": \"00\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"podCode\": \"\",\n" +
                "    \"podDir\": \"\",\n" +
                "    \"podTyp\": \"\",\n" +
                "    \"materialLot\": \"\",\n" +
//                "    \"priority\": \"6\",\n" +
                "    \"agvCode\": \"\",\n" +
                "    \"taskCode\": \"\",\n" +
                "    \"data\": \"\"\n" +
                "}";
        return  param;
    }


    @Override
    public void callRCTUAgv(String type,String taskCode,List<TurnoverInventory> list1,String huojia,String a) {
        //查找入库周转区里面的货物进行6倍出库
        String response = "";
        String taskModelType = "T1";
        String taskType = "";
        if(type.equals("0")){//入库
            taskType = "CTU入库";
        }else if(type.equals("1")){//出库
            taskType = "CTU出库";
        }
        //首先判断进行入库的物料有几个
        //然后获取CTU的对应数量的空闲货位进行放货
        int size = list1.size();
        Map<String,Object> map = new HashMap<>();
        map.put("num","limit "+size);
        List<Map<String,Object>> maps = agvMapper.selectFreeStorages(map);
        int selectsize = maps.size();
        int result = size-selectsize;
        if(result!=0){
            //说明CTU库存已满，有剩余物料
            //需要将剩余物料存储等待任务，当有空闲仓位时候再入库
            List<TurnoverInventory> newList = list1.subList(selectsize,size);
            Gson gson = new Gson();
            String json1 = gson.toJson(newList);
            SixFreetask sixFreetask = new SixFreetask();
            sixFreetask.setType("1");
            sixFreetask.setTaskModeltype(taskCode);
            sixFreetask.setWuliaoInfo(json1.toString());
            sixFreetask.setStartp(a);
//                sixFreetask.setEndp();
            sixFreetask.setHuojia(huojia);
//                sixFreetask.setStartText();
//                sixFreetask.setEndText();
            sixFreetaskMapper.insertSixFreetask(sixFreetask);
        }
        int num = 0;
        String huojiatongyiCode = UUID.randomUUID().toString().substring(0, 20);
        for(int i=0;i<maps.size();i++){
            FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
            fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
            TurnoverInventory turnoverInventory = list1.get(i);
            String wuliaono = turnoverInventory.getWuliaoNo();
            String startPosition = turnoverInventory.getCangweiNo();

            //获取起始仓位的货架、列、排信息等
            Map<String,Object> map3 = new HashMap<>();
            map3.put("cangweino",startPosition);
            Map<String,Object> map2 = agvMapper.selectarrlieInfos(map3);


            fiveTaskinfo.setTaskStartPosition(startPosition);
            Map<String,Object> map1 = maps.get(i);
            String endPosition = map1.get("cangwei_no").toString();
            fiveTaskinfo.setTaskEndPosition(endPosition);
            fiveTaskinfo.setTaskType(taskType);

            String param = createCTUParamInfo(taskModelType,startPosition,endPosition);
            JSONObject json = JSONObject.parseObject(param);
            String reqCode = UUID.randomUUID().toString().substring(0, 20);
            json.fluentPut("reqCode", reqCode);
            json.fluentPut("reqTime", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
            json.fluentPut("taskCode", reqCode);

            fiveTaskinfo.setTaskCode(reqCode);
            fiveTaskinfo.setTaskStartInfo(json.toString());
             response = HttpUtils.sendRestPost(rcsUrl+"genAgvSchedulingTask",json);
            fiveTaskinfo.setTaskEndInfo("CTU货架agv执行---"+response);
            fiveTaskinfo.setStatus("待完成");
            fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
            fiveTaskinfo.setRemark(wuliaono+"---"+map2.get("regionid").toString()+"/"+map2.get("arrangeid").toString()+"/"+map2.get("lieid").toString()+"/"+map2.get("storageid").toString()+"---"+map1.get("regionid").toString()+"/"+map1.get("arrangeid").toString()+"/"+map1.get("lieid").toString()+"/"+map1.get("storageid").toString()+"---"+(i+1)+"/"+maps.size()+"---"+a+"/"+huojia);
            fiveTaskinfo.setHuojiatongyiCode(huojiatongyiCode);
            fiveTaskinfoMapper.insertFiveTaskinfo(fiveTaskinfo);

            num = i;

            //将入库位绑定的仓位解绑，ctu仓位建立绑定关系
            bindcangweiornobind(startPosition,0l,1l);
            bindcangweiornobind(endPosition,1l,0l);
        }
        //需要判断当入库周转货架上的货物清空之后，要将此货架转回到入库位上装货
//        if(list1.size() == (num + 1)){
//            Long al = Long.parseLong(a);
//            String text = "";
//            if(a.equals("1")){
//                text = "入库周转区---> 入库位";
//            }else if(a.equals("4")){
//                text = "入库周转区---> 出库位";
//            }
//            operateFreeHuojia("Q2",2l,al,huojia,text,"普通背货架agv执行空货架搬运任务---",
//                    dibiaoMapper,fiveTaskinfoMapper);
//        }
    }

    public String createCTUParamInfo(String taskModelType,String startPosition,String endPosition){
        String param = "{\n" +
                "    \"reqCode\": \"\",\n" +
                "    \"reqTime\": \"\",\n" +
                "    \"clientCode\": \"\",\n" +
                "    \"tokenCode\": \"\",\n" +
                "    \"taskTyp\": \""+taskModelType+"\",\n" +
                "    \"ctnrTyp\": \"1\",\n" +
                "    \"ctnrCode\": \"\",\n" +
                "    \"wbCode\": \"\",\n" +
                "    \"positionCodePath\": [\n" +
                "        {\n" +
                "            \"positionCode\": \""+startPosition+"\",\n" +
                "            \"type\": \"05\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"positionCode\": \""+endPosition+"\",\n" +
                "            \"type\": \"05\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"podCode\": \"\",\n" +
                "    \"podDir\": \"\",\n" +
                "    \"podTyp\": \"\",\n" +
                "    \"materialLot\": \"\",\n" +
//                "    \"priority\": \"6\",\n" +
                "    \"agvCode\": \"\",\n" +
                "    \"taskCode\": \"\",\n" +
                "    \"data\": \"\"\n" +
                "}";
        return  param;
    }

    public static String createCCTUParamInfo(String taskModelType,String startPosition,String endPosition){
        String param = "{\n" +
                "    \"reqCode\": \"\",\n" +
                "    \"reqTime\": \"\",\n" +
                "    \"clientCode\": \"\",\n" +
                "    \"tokenCode\": \"\",\n" +
                "    \"taskTyp\": \""+taskModelType+"\",\n" +
                "    \"ctnrTyp\": \"1\",\n" +
                "    \"ctnrCode\": \"\",\n" +
                "    \"wbCode\": \"\",\n" +
                "    \"positionCodePath\": [\n" +
                "        {\n" +
                "            \"positionCode\": \""+startPosition+"\",\n" +
                "            \"type\": \"05\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"positionCode\": \""+endPosition+"\",\n" +
                "            \"type\": \"05\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"podCode\": \"\",\n" +
                "    \"podDir\": \"\",\n" +
                "    \"podTyp\": \"\",\n" +
                "    \"materialLot\": \"\",\n" +
//                "    \"priority\": \"6\",\n" +
                "    \"agvCode\": \"\",\n" +
                "    \"taskCode\": \"\",\n" +
                "    \"data\": \"\"\n" +
                "}";
        return  param;
    }

    /**
     * agv执行空货架搬运任务
     * */
    public static Map<String,Object> operateFreeHuojia(String taskModelType, Long startP, Long endP, String huojia, String taskType,
                                                       String taskEndInfo, SysDibiaoMapper dibiaoMapper, FiveTaskinfoMapper fiveTaskinfoMapper,SixFreetaskMapper sixFreetaskMapper){
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            //获取开始位置与技术位置
            Map<String,Object> map1 = gainweizhiInfo(huojia,startP,endP,dibiaoMapper);
            String startPosition = map1.get("startPosition").toString();
            String endPosition = map1.get("endPosition").toString();

            if(endPosition.equals("")){
                //需要将此操作记录在流程中，等到有空闲货架出现执行搬运任务
                SixFreetask sixFreetask = new SixFreetask();
                sixFreetask.setType("3");
                sixFreetask.setTaskModeltype(taskModelType);
//                sixFreetask.setWuliaoInfo(json1.toString());
                sixFreetask.setStartp(startP.toString());
                sixFreetask.setEndp(endP.toString());
                sixFreetask.setHuojia(huojia);
                sixFreetask.setStartText(taskType);
                sixFreetask.setEndText(taskEndInfo);
                sixFreetaskMapper.insertSixFreetask(sixFreetask);
                map.put("code",200);
                map.put("msg","操作成功！");
                return map;
            }

            //说明货架上货物已被清空
            FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
            fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
            fiveTaskinfo.setTaskStartPosition(startPosition);
            fiveTaskinfo.setTaskEndPosition(endPosition);
            fiveTaskinfo.setTaskType(taskType);

            String param = createParamInfo(taskModelType,startPosition,endPosition);
            JSONObject json = JSONObject.parseObject(param);
            String reqCode = UUID.randomUUID().toString().substring(0, 20);
            json.fluentPut("reqCode", reqCode);
            json.fluentPut("reqTime", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
            json.fluentPut("taskCode", reqCode);

            fiveTaskinfo.setTaskCode(reqCode);
            fiveTaskinfo.setTaskStartInfo(json.toString());
            String response = "";
            response = HttpUtils.sendRestPost(rcsUrl+"genAgvSchedulingTask",json);
            fiveTaskinfo.setTaskEndInfo(taskEndInfo+response);
            fiveTaskinfo.setStatus("待完成");
            fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
            fiveTaskinfo.setRemark(huojia);
            fiveTaskinfoMapper.insertFiveTaskinfo(fiveTaskinfo);
            map.put("code",200);
            map.put("msg","操作成功！");
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",100);
            map.put("msg",e.getMessage());
        }
        return map;
    }
    /**获取开始位置与结束位置*/
    public static Map<String ,Object> gainweizhiInfo(String huojiano,Long startP,Long endP,SysDibiaoMapper dibiaoMapper){
        Map<String,Object> map = new HashMap<>();
        String startPosition= "";
        String endPosition = "";
        SysDibiao sysDibiao = new SysDibiao();
        sysDibiao.setType(startP);
        sysDibiao.setHuojia(huojiano);
        List<SysDibiao> dblist = dibiaoMapper.selectSysDibiaoList(sysDibiao);
        startPosition = dblist.get(0).getName();
        sysDibiao =  new SysDibiao();
        sysDibiao.setType(endP);
        List<SysDibiao> dblists = dibiaoMapper.selectSysFreeDibiaoList(sysDibiao);
        if(dblists.size()!=0){
            sysDibiao = dblists.get(0);
            //对取得的结束位置预占用
            sysDibiao.setIsFree("1");
            dibiaoMapper.updateSysDibiao(sysDibiao);
            endPosition = sysDibiao.getName();
        }
        map.put("startPosition",startPosition);
        map.put("endPosition",endPosition);
        return map;
    }

    /**仓位绑定与解绑*/
    public void bindcangweiornobind(String position,long isrequest,long isfree){
        SysStorage sysStorage = new SysStorage();
        sysStorage.setCangweiNo(position);
        List<Map<String,Object>> list = sysStorageMapper.selectSysStorageList(sysStorage);
        Map<String,Object> map5 = list.get(0);
        sysStorage.setCangweiNo(null);
        sysStorage.setId(Long.parseLong(map5.get("id").toString()));
        sysStorage.setIsRequest(isrequest);
        sysStorage.setIsFree(isfree);
        sysStorageMapper.updateSysStorage(sysStorage);
    }
    /**ctu出库*/
    public static Map<String,Object> callCCTUAgv(List<TurnoverInventory> list,FiveCtuRecordMapper fiveCtuRecordMapper,
                                   SysStorageMapper sysStorageMapper,AgvMapper agvMapper,FiveTaskinfoMapper fiveTaskinfoMapper,SysDibiaoMapper dibiaoMapper){
        Map<String,Object> map1 = new HashMap<String,Object>();
        try{
            String response = "";
            String taskModelType = "T2";
            String taskType = "CTU出库";
            int num = 0;
            String huojia = "";
            String huojiatongyiCode = UUID.randomUUID().toString().substring(0, 20);
            for(int i=0;i<list.size();i++){
                TurnoverInventory turnoverInventory = list.get(i);

                FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
                fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));

                String wuliaono = turnoverInventory.getWuliaoNo();
                FiveCtuRecord fiveCtuRecord = new FiveCtuRecord();
                fiveCtuRecord.setWuliaoCode(wuliaono);
                List<FiveCtuRecord> list1 = fiveCtuRecordMapper.selectFiveCtuRecordList(fiveCtuRecord);
                Map<String,Object> map = sysStorageMapper.selectSysStorageById(list1.get(0).getStorageId());
                String startPosition = map.get("cangweiNo").toString();
                String endPosition = turnoverInventory.getCangweiNo();

                huojia = endPosition.substring(0,6);

                Map<String,Object> map3 = new HashMap<>();
                map3.put("cangweino",endPosition);
                Map<String,Object> map2 = agvMapper.selectarrlieInfos(map3);

                fiveTaskinfo.setTaskStartPosition(startPosition);
                fiveTaskinfo.setTaskEndPosition(endPosition);
                fiveTaskinfo.setTaskType(taskType);

                String param = createCCTUParamInfo(taskModelType,startPosition,endPosition);
                JSONObject json = JSONObject.parseObject(param);
                String reqCode = UUID.randomUUID().toString().substring(0, 20);
                json.fluentPut("reqCode", reqCode);
                json.fluentPut("reqTime", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
                json.fluentPut("taskCode", reqCode);

                fiveTaskinfo.setTaskCode(reqCode);
                fiveTaskinfo.setTaskStartInfo(json.toString());
                response = HttpUtils.sendRestPost(rcsUrl+"genAgvSchedulingTask",json);
                fiveTaskinfo.setTaskEndInfo("CTU货架agv执行---"+response);
                fiveTaskinfo.setStatus("待完成");
                fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                fiveTaskinfo.setRemark(map.get("wuliaoCode").toString()+"---"+map.get("regionId").toString()+"/"+map.get("arrangeId").toString()+"/"+map.get("lieId").toString()+"/"+map.get("id").toString()+"---"+map2.get("regionid").toString()+"/"+map2.get("arrangeid").toString()+"/"+map2.get("lieid").toString()+"/"+map2.get("storageid").toString()+"---"+(i+1)+"/"+list.size()+"---"+huojia);
                fiveTaskinfo.setHuojiatongyiCode(huojiatongyiCode);
                fiveTaskinfoMapper.insertFiveTaskinfo(fiveTaskinfo);

                bindcangweiornobind1(startPosition,0l,1l,sysStorageMapper);
                bindcangweiornobind1(endPosition,1l,0l,sysStorageMapper);

                num = i;
            }
            //需要判断当入库周转货架上的货物清空之后，要将此货架转回到入库位上装货
//            if(list.size() == (num + 1)){
//                Map<String,Object> map2 = operateFreeHuojia("Q5",3l,4l,huojia,"出库周转区---> 出库位","普通背货架agv执行满货架搬运任务---",dibiaoMapper,fiveTaskinfoMapper);
//                return map2;
//            }
            map1.put("code",200);
            map1.put("msg","操作成功！");
        }catch (Exception e){
            e.printStackTrace();
            map1.put("code",100);
            map1.put("msg","操作失败！");
        }
        return map1;
    }
    public static void bindcangweiornobind1(String position,long isrequest,long isfree,SysStorageMapper sysStorageMapper){
        SysStorage sysStorage = new SysStorage();
        sysStorage.setCangweiNo(position);
        List<Map<String,Object>> list = sysStorageMapper.selectSysStorageList(sysStorage);
        Map<String,Object> map5 = list.get(0);
        sysStorage.setCangweiNo(null);
        sysStorage.setId(Long.parseLong(map5.get("id").toString()));
        sysStorage.setIsRequest(isrequest);
        sysStorage.setIsFree(isfree);
        sysStorageMapper.updateSysStorage(sysStorage);
    }

    public static String bangding(String huojia,String dibiao,int indBind){
        JSONObject json = new JSONObject();
        String reqCode = UUID.randomUUID().toString().substring(0, 20);
        json.put("reqCode",reqCode);
        json.put("reqTime",DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
        json.put("clientCode","");
        json.put("tokenCode","");
        json.put("podCode",huojia);
        json.put("positionCode",dibiao);
        json.put("podDir","0");
        json.put("characterValue","0");
        json.put("indBind",indBind);
        String response = HttpUtils.sendRestPost(rcsUrl+"bindPodAndBerth",json);
        System.out.println("绑定或解绑==============="+response);
        return response;
    }
    public static String gainRorCweizhiInfo(String huojiano,SysDibiaoMapper dibiaoMapper){
        String a = "";
        SysDibiao sysDibiao = new SysDibiao();
        sysDibiao.setHuojia(huojiano);
        List<SysDibiao> dblist = dibiaoMapper.selectSysDibiaoList(sysDibiao);
        a = ""+dblist.get(0).getType();
        return a;
    }
    public static Map<String, Object> boxBack(String huojiahao,SysStorageMapper sysStorageMapper,SixWuliaoinfoMapper sixWuliaoinfoMapper) {
        Map<String, Object> map = new HashMap<>();
//        String huojiahao = cangweiNo.substring(0, 6);
        SysStorage sysStorage = new SysStorage();
        sysStorage.setLieType(huojiahao);
        List<Map<String,Object>> lists = sysStorageMapper.selectSysStorageList(sysStorage);
        if(lists.size()!=0){
            for(Map<String,Object> map1 : lists){
                String aa = gainStr(map1.get("cangweiNo").toString());
                System.out.println("仓位解绑接口返回值======="+aa);
            }
            //将货架关联的物料信息清空，物料溯源结束
            sixWuliaoinfoMapper.deleteSixWuliaoinfoByhuojiacode(huojiahao);
            //将出库货架所有关联货位状态为free
            bindcangweiornobind2(huojiahao, 0, 0,sysStorageMapper);
            // TODO  strName为出库位货架地标号，endName 终点位置
            map.put("code", 200);
            map.put("msg", "执行成功！");
        }else{
            map.put("code", 100);
            map.put("msg", "请正确输入仓位编号！");
        }
        return map;
    }
    public static void bindcangweiornobind2(String position, long isrequest, long isfree,SysStorageMapper sysStorageMapper) {
        SysStorage sysStorage = new SysStorage();
        sysStorage.setLieType(position);
        List<Map<String, Object>> list = sysStorageMapper.selectSysStorageList(sysStorage);
        for (Map<String, Object> map : list) {
            sysStorage.setCangweiNo(null);
            sysStorage.setId(Long.parseLong(map.get("id").toString()));
            sysStorage.setIsRequest(isrequest);
            sysStorage.setIsFree(isfree);
            sysStorageMapper.updateSysStorage(sysStorage);
        }
    }
}
